package com.ruoyi.st.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.response.JsonResult;
import com.ruoyi.st.domain.query.AudienceQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.st.domain.StAudience;
import com.ruoyi.st.service.IStAudienceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import springfox.documentation.spring.web.json.Json;

/**
 * 观演人Controller
 * 
 * @author ruoyi
 * @date 2024-11-16
 */
@Slf4j
@RestController
@RequestMapping("/st/audience")
public class StAudienceController extends BaseController
{
    @Autowired
    private IStAudienceService stAudienceService;

    /**
     * 查询观演人列表
     */
//    @PreAuthorize("@ss.hasPermi('st:audience:list')")
    @GetMapping("/list")
    public TableDataInfo list(StAudience stAudience)
    {

        startPage();
        List<StAudience> list = stAudienceService.selectStAudienceList(stAudience);
        return getDataTable(list);
    }

    /**
     * 通过userId查询对应的观演人列表
     * @Param userId
     * @return List<StAudience>
     */
    @GetMapping("/listByUserId/{userId}")
   public JsonResult selectListByUserId(@PathVariable("userId") Long userId) {
        if (userId == null) {
            throw new IllegalArgumentException("User ID is required");
        }
        List<StAudience> stAudiencesList = stAudienceService.selectStAudienceListByUserId(userId);
        return JsonResult.ok(stAudiencesList);
    }

    /**
     * 导出观演人列表
     */
    @PreAuthorize("@ss.hasPermi('st:audience:export')")
    @Log(title = "观演人", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StAudience stAudience)
    {
        List<StAudience> list = stAudienceService.selectStAudienceList(stAudience);
        ExcelUtil<StAudience> util = new ExcelUtil<StAudience>(StAudience.class);
        util.exportExcel(response, list, "观演人数据");
    }

    /**
     * 获取观演人详细信息
     */
//    @PreAuthorize("@ss.hasPermi('st:audience:query')")
    @GetMapping(value = "/{id}")
    public JsonResult getInfo(@PathVariable("id") Long id)
    {
        StAudience stAudience = stAudienceService.selectStAudienceById(id);
        return JsonResult.ok(stAudience);
    }

    /**
     *  通过userId 和 id查询观演人信息
     * @Param id
     * @Param userId
     */
    @PostMapping(value = "/queryByIdAndUserId")
    public JsonResult getAudienceByUserIdAndId(@RequestBody AudienceQuery audienceQuery) {
        log.debug("传过来的观演人id和用户id: id={}, userId={}", audienceQuery.getId(), audienceQuery.getUserId());
        StAudience stAudience = stAudienceService.selectStAudienceListByUserIdAndId(audienceQuery);
        return JsonResult.ok(stAudience);
    }

    /**
     * 新增观演人
     */
//    @PreAuthorize("@ss.hasPermi('st:audience:add')")
//    @Log(title = "观演人", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody StAudience stAudience)
    {
        return toAjax(stAudienceService.insertStAudience(stAudience));
    }

    /**
     * 修改观演人
     */
//    @PreAuthorize("@ss.hasPermi('st:audience:edit')")
    @Log(title = "观演人", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody StAudience stAudience)
    {
        return toAjax(stAudienceService.updateStAudience(stAudience));
    }



    /**
     * 删除观演人
     */
//    @PreAuthorize("@ss.hasPermi('st:audience:remove')")
    @Log(title = "观演人", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public JsonResult remove(@PathVariable Long[] ids)
    {
        return JsonResult.ok(stAudienceService.deleteStAudienceByIds(ids));
    }

    @DeleteMapping("/del/{id}")
    public JsonResult removeById(@PathVariable Long id)
    {
        return JsonResult.ok(stAudienceService.deleteStAudienceById(id));
    }
}
